<?php
/**购物车管理*/
namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Shopcart extends Model
{
    protected $table='cart';

    /**
     * 购物车列表
     * @userid 当前登陆用户id
     */
    public static function lists($userid)
    {
        $res = DB::table('cart as ct')
            ->join('fitment_details as dt','ct.cart_fitment_id','=','dt.details_id')
            ->leftJoin('fitments as ft','ft.fitments_id','=','dt.details_pid')
            ->join('details_img as img','ct.cart_fitment_id','=','img.fitment_details_id')
            ->leftJoin('fitment_color as color','dt.details_id','=','color.color_details_id')
            ->where('cart_user_id',$userid)
            ->select(
                'dt.details_id','dt.details_pid','dt.details_bond','dt.details_fixprice','dt.details_nimbleprice', 'dt.details_name', 'dt.details_length','dt.details_width',
                'ft.fitments_name','color.color_type',
                'img.fitment_details_imgOne','img.fitment_details_imgMaster',
                'ct.cart_fitment_num','ct.cart_id','ct.cart_rent_type','ct.cart_status'
            )
            ->get();
        return $res;
    }

    public static function lists_status($userid)
    {
        $res = DB::table('cart as ct')
            ->join('fitment_details as dt','ct.cart_fitment_id','=','dt.details_id')
            ->leftJoin('fitments as ft','ft.fitments_id','=','dt.details_pid')
            ->join('details_img as img','ct.cart_fitment_id','=','img.fitment_details_id')
            ->where('cart_user_id',$userid)
            ->where('cart_status',1)
            ->select('dt.details_id','dt.details_pid','dt.details_bond','dt.details_fixprice','dt.details_nimbleprice',
                'dt.details_name','dt.details_length','dt.details_width','ft.fitments_name',
                'img.fitment_details_imgOne','ct.cart_fitment_num','ct.cart_id','ct.cart_rent_type','ct.cart_status')
            ->get();
        return $res;
    }


    /**新增购物车*/
    public static function add($param)
    {
        /**查看是否是同类型添加*/
        $cart = DB::table('cart')
            ->where('cart_user_id',$param['cart_user_id'])
            ->where('cart_fitment_id',$param['cart_fitment_id'])
            ->where('cart_rent_type',$param['cart_rent_type'])
            ->first();
        if(count($cart)>0){
            $res = DB::table('cart')->where('cart_id',$cart->cart_id)->increment('cart_fitment_num',$param['cart_fitment_num']);
        } else {
            $res = DB::table('cart')->insertGetId($param);
        }
        return $res;
    }

    /***
     * 购物车编辑
     * @id
     * @param
     */
    public static function edit($id,$data)
    {
        $res = DB::table('cart')->where('cart_id',$id)->update($data);
        return $res;
    }

    /**购物车单个查询*/
    public static function cartdt($cartid)
    {
        $res =  DB::table('cart as ct')
            ->join('fitment_details as dt','ct.cart_fitment_id','=','dt.details_id')
            ->leftJoin('fitments as ft','dt.details_pid','=','ft.fitments_id')
            ->leftJoin('details_img as img','ct.cart_fitment_id','=','img.fitment_details_id')
            ->where('cart_id',$cartid)
            ->select('ct.*','dt.details_name','dt.details_length','dt.details_width','dt.details_fixprice',
                'dt.details_nimbleprice','dt.details_bond','ft.fitments_brand_id','ft.fitments_name','ft.fitments_describe',
                'ft.fitments_subcate_id','img.fitment_details_imgMaster','img.fitment_details_imgOne')
            ->first();
        return $res;
    }

    /**
     * 购物车删除
     * @id 购物车id
     */
    public static function destroy($id)
    {
        $res = DB::table('cart')->where('cart_id',$id)->delete();
        return $res;
    }


}
